//
//  AEHomePageSubViewModel.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageSubViewModel.h"

@implementation AEHomePageSubViewModel

- (void)initialize {
    [super initialize];
        
    _reqModel = [AENewsInfoReqModel new];
    _reqModel.pageNum = 1;
    _reqModel.pageSize = 10;
}

- (void)setupData {
    _reqModel.newsGroupId = @(self.newsGroupId);
}

/// 获取新闻列表
/// - Parameter completion: completion description
- (void)requestWithNewsInfos:(RequestCompletionBlock)completion {
    _reqModel.newsTitle = self.searchMsg;
    @weakify(self);
    AENewsListRequest *request = [[AENewsListRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
            NSArray *list = @[];
            if (respond.data && [respond.data isKindOfClass:[NSArray class]]) {
                list = respond.data;
            }
            
            if (self.reqModel.pageNum == 1) {
                [self.dataList removeAllObjects];
            }
            for (AENewsInfoModel *infoModel in list) {
                for (AENewsInfoOssModel *ossModel in infoModel.ossList) {
                    ossModel.url = [NSString stringWithFormat:@"%@%zd", kImageBaseUrl, ossModel.id];
                    [infoModel.ossUrls addObject:ossModel.url];
                }
                [self.dataList addObject:infoModel];
            }
            if (self.reqModel.pageSize == list.count) {
                self.reqModel.pageNum += 1;
            }
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 点赞/取消
/// - Parameters:
///   - newsId: 新闻id
///   - like: 1 赞；1 取消
///   - completion: completion description
- (void)requestWithNewsLikeNewsId:(NSInteger)newsId like:(NSInteger)like completion:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsLikeRequest *request = [[AENewsLikeRequest alloc] initWithNewsId:newsId like:like];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
